﻿using System;
using System.Collections.Generic;
using System.Text;
using Kitware.VTK;

namespace Fu3DEngine
{
    public class TubeLineNode : CPNodeBase 
    {
        CPTubeLine m_ent;
        public TubeLineNode(CPTubeLine ent,string id):base(id)
        {
            m_ent = ent;
        }

        public override void Run()
        {
            base.Run();
            this.CreateTubeLine(m_ent);


        }

        public override void ReLoad()
        {
            throw new NotImplementedException();
        }


        public void CreateTubeLine(CPTubeLine cy)
        {
            vtkPoints points = new vtkPoints();
            int id = 0;

            double[] dOrg = new double[3];

            foreach (TPoint dp in cy.m_points)
            {


                points.InsertPoint(id, dp.x, dp.y, dp.z);

                id++;


            }



            vtkCellArray polyarray = new vtkCellArray();
            polyarray.InsertNextCell(cy.m_points.Count);
            for (int i = 0; i < cy.m_points.Count; i++)
            {
                polyarray.InsertCellPoint(i);
            }

            vtkPolyData polyData = new vtkPolyData();
            polyData.SetPoints(points);
            polyData.SetLines(polyarray);
            double[] cen1 = polyData.GetCenter();

            //vtkExtractEdges ee = vtkExtractEdges.New();
            //ee.SetInput(polyData);

            vtkCleanPolyData cpd = vtkCleanPolyData.New();
            cpd.SetInput(polyData);


            //下面是关于转换tube的方法
            vtkTubeFilter tf2 = new vtkTubeFilter();
            tf2.CappingOn();
            tf2.SetNumberOfSides(cy.numSides);
            tf2.SetInput(cpd.GetOutput());
            tf2.SetRadius(cy.radius);


           // double temp = (cy.maxH - cy.minH) / 2 + cy.minH;
            double temp = cy.radius;


           

            vtkTransform tran = new vtkTransform();
            tran.Translate(0, 0, temp);


            vtkTransformFilter tf = new vtkTransformFilter();
            tf.SetInput(tf2.GetOutput());
            tf.SetTransform(tran);




            
            m_Poly.m_Mapper.SetInput(tf.GetPolyDataOutput());








          







            //actor.SetPosition(cy.x, cy.y, cy.z);

            m_Poly.m_Actor.SetPosition(0, 0, 0);

            m_Poly.m_Actor.GetProperty().SetAmbientColor(cy.m_color.R, cy.m_color.G, cy.m_color.B);
            m_Poly.m_Actor.GetProperty().SetDiffuseColor(cy.m_color.R, cy.m_color.G, cy.m_color.B);



            this.SParent.m_vtkRenderer.AddActor(m_Poly.m_Actor);




        }
    }
}
